﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Linq;

namespace Transportr
{
    public class LineDetailsViewModel : BaseItemViewModel
    {
        private LineDetailsItemViewModel mCurrentItem;
        private string mMessage;

        public LineDetailsModel LineDetails { get; private set; }
        public string query;

        public LineDetailsItemViewModel CurrentItem
        {
            get { return mCurrentItem; }
            set
            {
                if (value != mCurrentItem)
                {
                    mCurrentItem = value;
                    NotifyPropertyChanged("CurrentItem");
                }
            }
        }

        public Visibility ProgressVisibility
        {
            get
            {
                if (LineDetails.IsDataLoaded)
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;
            }
        }

        public string Message
        {
            get { return mMessage; }
            set
            {
                if (value != mMessage)
                {
                    mMessage = value;
                    NotifyPropertyChanged("Message");
                }
            }
        }

        public LineDetailsViewModel()
        {
            Message = "Loading...";
            LineDetails = new LineDetailsModel();
            LineDetails.Updated += new UpdatedHandler(LineDetails_Updated);
        }

        void LineDetails_Updated(string s)
        {
            if (query != null && query.Split(',').Length == 2)
            {
                this.CurrentItem.ReverseRoute = (Int32.Parse(query.Split(',')[1]) == 1);
                this.CurrentItem.CurrentStation = this.CurrentItem.CurrentRoute.First(i => i.Station.stId == Int32.Parse(query.Split(',')[0]));
            }
            Message = s;
            NotifyPropertyChanged("ProgressVisibility");
        }
    }
}